﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace keepscreen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Visible = false;
            this.Hide();

            InitMouse();
        }

        private void ToolStripMenuItem_BootStart_Click(object sender, EventArgs e)
        {
            //开机启动
            RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
                ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
            registryKey.SetValue("KeepScreen", Application.ExecutablePath);
        }
        bool EnableKeepStr = true;

        private void ToolStripMenuItem_KeepScr_Click(object sender, EventArgs e)
        {
            EnableKeepStr = !EnableKeepStr;

            InitMouse();

            //StartKeepScreen
        }

        private void TimeToolStripMenuItem_mouseStop_Click(object sender, EventArgs e)
        {
            //MouseStop info
        }
        float InputIdleTime = 0;
        bool MouseUpdate;
        DateTime MouseLastTime = DateTime.MinValue;
        void InitMouse()
        {
            InputIdleTime = 0;
            MouseLastTime = DateTime.Now;
            ToolStripMenuItem_KeepScr.Text = "防锁屏=" + EnableKeepStr;
        }
        Point lastPoint;

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out Point lpPoint);
        void DoInput()
        {
            try
            {
                SendKeys.Send("{NUMLOCK}");
                SendKeys.Flush();
                SendKeys.Send("{NUMLOCK}");
                SendKeys.Flush();
            }
            catch
            {

            }
            //平移 纵移一下鼠标，然后计时器归零
            var oldp = System.Windows.Forms.Cursor.Position;
            if (lastPoint == oldp)
            {
                MouseUpdate = true;

                Point newp1 = oldp;
                Point newp2 = oldp;
                if (newp1.X > 100)
                    newp1.X -= 100;
                else
                    newp1.X += 100;

                if (newp2.Y > 100)
                    newp2.Y -= 100;
                else
                    newp2.Y += 100;
                SetCursorPos(newp1.X, newp1.Y);
                
                SetCursorPos(newp2.X, newp2.Y);

                SetCursorPos(oldp.X, oldp.Y);

            }
            else
            {
               
                MouseUpdate = false;
            }
            lastPoint = oldp;
            InputIdleTime = 0;
        }
        void UpdateMouse()
        {
            var time = DateTime.Now;
            var delta = (time - MouseLastTime).TotalSeconds;
            MouseLastTime = time;

            InputIdleTime += (float)delta;
            TimeToolStripMenuItem_mouseStop.Text = "InputIdle=" + (int)InputIdleTime + " Update=" + MouseUpdate;
            if (InputIdleTime > 15)
            {
                //每60秒动一下鼠标，防止锁屏
                DoInput();
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (EnableKeepStr)
            {
                UpdateMouse();
            }
        }
    }
}
